const { model } = require("mongoose");
const { auditParkings, getState} = require("../dao/parkingsDao")
//车位数据渲染
const { getparkings } = require("../dao/parkingsDao")
const { searchgetparkings } = require("../dao/parkingsDao")
module.exports.getparkings = async function (park) {
    if (park.seatchtype) {
        const getparks = await searchgetparkings(park)
        return { getparks, message: '数据保存成功', status: 1 };
    } else {
        const getparks = await getparkings(park)
        return { getparks, message: '数据保存成功', status: 1 };
    }
}

//车位软删除
const { delparkings } = require("../dao/parkingsDao");
module.exports.delparkings = async function (park) {
    const delpark = await delparkings(park)
    if (delpark) {
        return { message: '数据删除成功', status: 1 };
    }
}

//修改渲染
const { xgindex } = require("../dao/parkingsDao");
module.exports.xgindex = async function (park) {
    const data = await xgindex(park);
    return { data, message: '数据获取成功', status: 1 }
}

const { addParkings } = require('../dao/parkingsDao')
module.exports.addParkings = async function (newParking) {
    const data = addParkings(newParking)
    if (data) {
        return {
            status: 1,
            message: '添加成功'
        }
    }
    return {
        status: 0,
        message: '添加失败'
    }
}

//修改车位
const { xginPark } = require("../dao/parkingsDao");
module.exports.xginPark = async function (park) {
    const xgpark = await xginPark(park)
    return { message: '数据修改成功', status: 1 }
}


module.exports.auditParkings = async function (msg) {
    const { searchType, searchValue, currentPage, pageSize } = msg
    const data = await auditParkings(msg);
    const data1 = data.filter(item => {

        switch (searchType) {
            case "address":

                if (item.address.includes(searchValue)) {
                    return item
                }
                break;

            case "lessorsId.username":
                if (item.lessorsId.username.includes(searchValue)) {
                    return item
                }
                break;
            case "carNumber":
                if (item.carNumber.includes(searchValue - 0)) {
                    return item
                }

                break;


        }
    })
    //分页
    const allPages = data1.length
    let data2 = []
    if (pageSize * currentPage > data1.length) {
        data2 = data1.slice((currentPage - 1) * pageSize)
    } else {
        data2 = data1.slice((currentPage - 1) * pageSize, currentPage * pageSize)
    }
    return { data2, allPages, status: 1 }
}


module.exports.getState = async function (msg) {
    const data = await getState(msg);
    if (data) {
        return {
            status: '1',
        }
    } else {
        return {
            status: '0'
        }
    }
  }